<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 考试 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
                <el-input v-model="query.keyword" placeholder="输入试卷名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column label="用户头像" width="100">
                    <template slot-scope="scope">
                        <img :src="scope.row.avatar" @error="tabErrorPicture(scope.row.id)" alt="" class="info-image" v-if="scope.row.avatar" />
                        <img :src="errorProduct" alt="" class="info-image" v-else />
                    </template>
                </el-table-column>
                <el-table-column prop="nickname" label="用户名称"></el-table-column>
                <el-table-column prop="title" label="试卷"></el-table-column>
                <el-table-column label="用时">
                    <template slot-scope="scope">
                        <span>{{
                            10 > Math.floor(scope.row.total_time / 3600)
                                ? '0' + Math.floor(scope.row.total_time / 3600)
                                : Math.floor(scope.row.total_time / 3600)
                        }}</span
                        >:<span>{{
                            10 > Math.floor((scope.row.total_time / 60) % 60)
                                ? '0' + Math.floor((scope.row.total_time / 60) % 60)
                                : Math.floor((scope.row.total_time / 60) % 60)
                        }}</span
                        >:<span>{{
                            10 > Math.floor(scope.row.total_time % 60)
                                ? '0' + Math.floor(scope.row.total_time % 60)
                                : Math.floor(scope.row.total_time % 60)
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="成绩">
                    <template slot-scope="scope">{{ scope.row.score }} 分</template>
                </el-table-column>
                <el-table-column prop="create_time" label="考试时间"></el-table-column>
                <el-table-column label="操作" width="200" align="center" fixed="right">
                    <template slot-scope="scope">
                        <el-button @click="answer_detail(scope.row.id, scope.row.title)" type="success" size="mini">答题详情</el-button>
                        <el-button @click="deleteRow(scope.row.id, scope.$index)" type="danger" size="mini">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.rows"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import { examLogListData, delExamLogData } from '../../api/index';
export default {
    // name: 'lecturer',
    data() {
        return {
            query: {
                page: 1,
                rows: 4,
                keyword: ''
            },

            tableData: [],
            multipleSelection: [],
            delList: [],
            pageTotal: 0,
            errorProduct: require('@/assets/img/noProduct.jpg'), 
        };
    },
    created() {
        this.getData();
    },
    methods: {
        tabErrorPicture(id){    // 表格图文封面默认展示图
            let i = this.tableData.findIndex(v => v.id == id);
			this.tableData[i].avatar = this.errorProduct;
        },
        getData() {
            examLogListData(this.query).then((res) => {
                this.tableData = res.data.list;
                this.pageTotal = res.data.count;
            });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        //批量删除
        delAllSelection() {
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                const length = this.multipleSelection.length;
                let str = '';
                let strID = '';
                this.delList = this.delList.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].title + ' ';
                    strID += this.multipleSelection[i].id + ',';
                }
                delExamLogData({ id: strID.substring(0, strID.lastIndexOf(',')) }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success(`删除了${str}`);
                        this.multipleSelection = [];
                        setTimeout(() => {
                            location.reload();
                        }, 200);
                    }
                });
            });
        },
        // 搜索
        handleSearch() {
            this.$set(this.query);
            this.getData();
        },

        //答题详情
        answer_detail(id, title) {
            console.log(id,title)
            this.$router.push({ path: '/aidTool/answerDetail', query: { id, title } });
        },

        //删除
        deleteRow(id, index) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                delExamLogData({ id: id }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success('删除成功');
                        this.tableData.splice(index, 1);
                        this.getData();
                    }
                });
            });
        },

        // 分页
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
    float: right;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}

.mr10 {
    margin-right: 10px;
}

.info-image {
    width: 80px;
    border-radius: 4px;
}
</style>




